package book;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 87111
 * Date: 2025-08-21
 * Time: 20:40
 */
public class Book implements Comparable<Book> {
    private int bookId; //书Id
    private String bookName;   //书名
    private String author;  //作者
    private String category;    //类别
    private int publishYear;    //出版年份
    private boolean isBorrowed; //借阅状态
    private int borrowCount;    //借阅次数
    private LocalDate shelfDate;    //上架时间

    //构造方法，初始化Book类对应的成员变量
    public Book(String bookName, String author, String category,
                int publishYear, LocalDate shelfDate) {
        this.bookId = bookId;
        this.bookName = bookName;
        this.author = author;
        this.category = category;
        this.publishYear = publishYear;
        this.isBorrowed = false;
        this.borrowCount = 0;
        this.shelfDate = shelfDate;
    }

    public int getBookId() {
        return bookId;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public int getPublishYear() {
        return publishYear;
    }

    public void setPublishYear(int publishYear) {
        this.publishYear = publishYear;
    }

    public boolean getIsBorrowed() {
        return isBorrowed;
    }

    public void setIsBorrowed(boolean borrowed) {
        isBorrowed = borrowed;
    }

    public int getBorrowCount() {
        return borrowCount;
    }

    public void setBorrowCount(int borrowCount) {
        this.borrowCount = borrowCount;
    }

    public LocalDate getShelfDate() {
        return shelfDate;
    }

    public void setShelfDate(LocalDate shelfDate) {
        this.shelfDate = shelfDate;
    }

    //借书借阅次数加1
    public void borrowBook() {
        borrowCount++;
    }
    //还书减1
    public void returnBook() {
        borrowCount--;
     }
    @Override
    public String toString() {
        return "Book{" +
                "bookId=" + bookId +
                ", bookName='" + bookName + '\'' +
                ", author='" + author + '\'' +
                ", category='" + category + '\'' +
                ", publishYear=" + publishYear +
                ", isBorrowed=" + isBorrowed +
                ", borrowCount=" + borrowCount +
                ", shelfDate=" + shelfDate +
                '}';
    }

    //将创建的对象书中的属性转化为字符串
    public String toJson() {
        StringBuilder bookString = new StringBuilder();
        bookString.append(bookId).append(",");
        bookString.append(bookName).append(",");
        bookString.append(author).append(",");
        bookString.append(category).append(",");
        bookString.append(publishYear).append(",");
        bookString.append(isBorrowed).append(",");
        bookString.append(borrowCount).append(",");
        bookString.append(shelfDate != null ?
                shelfDate.format(DateTimeFormatter.ISO_LOCAL_DATE) : "null");
        return bookString.toString();
    }
    public int compareTo(Book o) {
        return o.getBorrowCount() - this.getBorrowCount();
    }
}
